#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "BlockQueue.hpp"

void* production(void* args)
{
   while (true)
   {
      BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);
      int obj = rand() % 100;
      bq->push(obj);
      std::cout << "product a obj: " << obj << std::endl;
   }
   return nullptr;
}

void* consumption(void* args)
{
   while (true)
   {
      BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);
      int obj = bq->pop();
      std::cout << "consume a obj: " << obj << std::endl;
      sleep(1);
   }
   return nullptr;
}

int main()
{
   srand((time_t)time(nullptr));
   BlockQueue<int> bq(5, 2);
   
   pthread_t producer;
   pthread_create(&producer, nullptr, production, (void*)&bq);

   pthread_t consumer;
   pthread_create(&consumer, nullptr, consumption, (void*)&bq);

   pthread_join(producer, nullptr);
   pthread_join(consumer, nullptr);
   return 0;
}
